<?php

 include_once "sql.php";
 include_once "features.php";

// this function adds a specific ( image, algorithm ) pair to the database
// the offset is added to the feature coordinates to avoid negative values
 function to_index( $filename, $pageid = 0, $algo = 0, $offset = 5 ){
   $f = get_features( $filename, $algo, true );
   $ret = array();
   foreach( $f as $v ){
     $v = explode(" ", $v);
     $hex = '';
     foreach($v as $k => $vv){
       if( $k == 0 ) continue;
       $hex .= dechex($vv+$offset);
     }
     if( $v[0] > 255 ) $v[0] = 255;
     $r = array( $hex, str_pad( dechex($pageid), 10, '0', STR_PAD_LEFT ) . str_pad( dechex($v[0]), 2, '0', STR_PAD_LEFT ) );
     $ret[] = $r;
   }
   foreach($ret as $v){
     $q1 = "SELECT HEX(`key`), HEX(`page_ids`) FROM patterns WHERE `key` = UNHEX('$v[0]') AND `algo` = $algo;";
     $q2 = "INSERT INTO patterns VALUES (UNHEX('$v[0]'), UNHEX('$v[1]'), $algo);";
//echo "$q1\n$q2\n\n";
     $l = sqlQuery( $q1, array("HEX(`key`)", "HEX(`page_ids`)") );
     $pageids = "";
     foreach( $l as $vv ){
       if( strlen($vv["HEX(`page_ids`)"]) < 980 ) $pageids = $vv["HEX(`page_ids`)"];
     }
     $q3 = "UPDATE patterns SET `page_ids`=concat(`page_ids`, UNHEX('$v[1]')) WHERE `key`=UNHEX('$v[0]')  AND `page_ids`=UNHEX('$pageids') AND `algo`=$algo;";
     if( count($l) == 0 && $pageids == "" )
        sqlQuery( $q2 );
     else
        sqlQuery( $q3 );
   }
 }
// same but from url
 function url_to_index( $url, $pageid ){
   $data = "";
   if( ($data = file_get_contents( $url )) == false ) return;
   file_put_contents( sha1($url), $data );
   to_index( sha1($url), $pageid );
   exec("rm " . sha1($url));
 }

// this function returns a sorted array of candidates for a search with an estimated probability for each entry
 function search( $filename, $algo = 0, $offset = 5 ){
   $f = get_features( $filename, $algo, false );
   $ret = array();
   $frv = array();
   foreach( $f as $v ){
     $v = explode(" ", $v);
     $hex = '';
     foreach($v as $k => $vv){
       if( $k == 0 ) continue;
       $hex .= dechex($vv+$offset);
     }
     if( $v[0] > 255 ) $v[0] = 255;
     $ret[] = $hex;
     $frv[ strtoupper($hex) ] = $v[0];
   }
   $q = "SELECT HEX(`key`), HEX(`page_ids`) FROM patterns WHERE `algo`=$algo AND (";
   foreach($ret as $k => $v){
// limitat?
     if($k != 0) $q .= " OR ";
     $q .= " `key` = UNHEX('" . $v . "') ";
   }
   $q .= ");";
   $l = sqlQuery( $q, array("HEX(`key`)", "HEX(`page_ids`)") );

   $score = array();
   foreach( $l as $v ){
     $key = $v["HEX(`key`)"];
     $v = str_split( $v["HEX(`page_ids`)"], 12 );
     foreach( $v as $k => $id_frv ){
       $v[ $k ] = str_split( $id_frv, 10 );
       $v[ $k ][ 0 ] = hexdec( $v[ $k ][ 0 ] );
       $v[ $k ][ 1 ] = hexdec( $v[ $k ][ 1 ] );
       $diff = $v[$k][1] - $frv[$key];
       if( isset($frv[$key]) )
       $score[ $v[ $k ] [ 0 ] ] += 1 / (
          count( $f ) * ( 1 + max(pow($diff,3), sqrt(max($diff,0)) ) )
       );
     }
     if( !isset($score[ $v[ $k ] [ 0 ] ]) ) $score[ $v[ $k ] [ 0 ] ] = 0;
  }

  arsort( $score );
  return $score;
}

// this variant of the function searches by a pgm data
 function search_by_pgm( $pgm, $offset = 5 ){
   $f = get_features_by_pgm( $pgm );
   $ret = array();
   $frv = array();
   foreach( $f as $v ){
     $v = explode(" ", $v);
     $hex = '';
     foreach($v as $k => $vv){
       if( $k == 0 ) continue;
       $hex .= dechex($vv+$offset);
     }
     if( $v[0] > 255 ) $v[0] = 255;
     $ret[] = $hex;
     $frv[ strtoupper($hex) ] = $v[0];
   }
   $q = "SELECT HEX(`key`), HEX(`page_ids`) FROM patterns WHERE `algo`=0 AND (";
   foreach($ret as $k => $v){
// limitat?
     if($k != 0) $q .= " OR ";
     $q .= " `key` = UNHEX('" . $v . "') ";
   }
   $q .= ");";
   $l = sqlQuery( $q, array("HEX(`key`)", "HEX(`page_ids`)") );
   $score = array();
   foreach( $l as $v ){
     $key = $v["HEX(`key`)"];
     $v = str_split( $v["HEX(`page_ids`)"], 12 );
     foreach( $v as $k => $id_frv ){
       $v[ $k ] = str_split( $id_frv, 10 );
       $v[ $k ][ 0 ] = hexdec( $v[ $k ][ 0 ] );
       $v[ $k ][ 1 ] = hexdec( $v[ $k ][ 1 ] );
       $diff = $v[$k][1] - $frv[$key];
       if( isset($frv[$key]) )
       $score[ $v[ $k ] [ 0 ] ] += 1 / (
          count( $f ) * ( 1 + max(pow($diff,3), sqrt(max($diff,0)) ) )
       );
     }
     if( !isset($score[ $v[ $k ] [ 0 ] ]) ) $score[ $v[ $k ] [ 0 ] ] = 0;
  }

  arsort( $score );
  return $score;
}

?>
